home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / usr / sybase / sample / dblibrary / example9.c < prev    next >
C/C++ Source or Header  |  1993-04-22  |  7KB  |  284 lines

  1. /*
  2.  *    example9.c
  3.  *
  4.  * This example illustrates the use of UNIX-specific routines 
  5.  * to give applications greater control over the I/O blocking
  6.  * behavior of DB-Library.
  7.  */
  8.  
  9. /* The variables SYSV, BSD42, AIX, HP9000_300
  10.  * and HP9000_800 are defined in sybfront.h.
  11.  */
  12. #include <sybfront.h>
  13. #include <sybdb.h>
  14.  
  15. #if BSD42                       /* System BSD 4.2 */
  16. #define POLL_CALL 0
  17. #include <sys/time.h>
  18. #endif
  19.  
  20. #if SYSV && !(HP9000_300 || HP9000_800 || AIX)   /* System V (not HP-UX) */
  21. #define POLL_CALL 1
  22. #include <stropts.h>
  23. #include <poll.h>
  24. #define getdtablesize() (_NFILE)
  25. #endif
  26.  
  27. #if SYSV && (HP9000_300 || HP9000_800)          /* System V (HP-UX) */
  28. #define POLL_CALL 0
  29. #include <stdio.h>
  30. #include <sys/time.h>
  31. #define getdtablesize() (_NFILE)
  32. #endif
  33.  
  34. #if AIX                         /* System AIX */
  35. #define POLL_CALL 0
  36. #include <sys/select.h>
  37. #endif
  38.  
  39. #define DATELEN 26
  40.  
  41. /* Forward declarations of the error-handler, message-handler, and 
  42.  * utility routines.
  43.  */
  44. void         do_something_useful();
  45. int          err_handler();
  46. int          msg_handler();
  47.  
  48. main(argc, argv)
  49. int          argc;
  50. char         *argv[];
  51. {
  52.     DBPROCESS       *dbproc;  /* Our connection with the SQL Server */
  53.     LOGINREC        *login;   /* Our login information. */
  54.  
  55.     /* These are the variables used to store the returning data. */
  56.  
  57.     DBCHAR          datebuf[DATELEN];
  58.     DBCHAR          namebuf[MAXNAME+1]; /* MAXNAME is defined in
  59.                                          * sybdb.h as the maximum
  60.                                          * length for names of 
  61.                                          * database objects, such 
  62.                                          * as tables, columns, and 
  63.                                          * procedures.
  64.                                          */
  65.  
  66.     /* Initialize DB-Library. */
  67.     if (dbinit() == FAIL)
  68.         exit(ERREXIT);
  69.  
  70.     /* Install the user-supplied error-handling and message-handling
  71.      * routines. They are defined at the bottom of this source file.
  72.      */
  73.     dberrhandle(err_handler);
  74.     dbmsghandle(msg_handler);
  75.  
  76.     login = dblogin();
  77.     DBSETLPWD(login, "server_password");
  78.     DBSETLAPP(login, "example9");
  79.  
  80.     dbproc = dbopen(login, NULL);
  81.  
  82.     /*
  83.      * We are going to retrieve some information, from a table
  84.      * named "sysdatabases", regarding names and creation dates
  85.      * of databases.
  86.      */
  87.  
  88.     /* First, put the command into the command buffer. */
  89.  
  90.     dbcmd(dbproc, "select name, crdate from master..sysdatabases");
  91.  
  92.     /* Now it's time to send the command to the SQL Server. If the
  93.      * network isn't yet ready for some reason, we can do something
  94.      * useful while we're waiting.
  95.      *
  96.      * In a real application, the program may use this opportunity to
  97.      * donate time to a concurrently running task or to monitor some
  98.      * other input stream.
  99.      *
  100.      * In this case, we'll just print out a message, stating that
  101.      * there's time available.
  102.      */
  103.  
  104.     while (ioselect(DBIOWDESC(dbproc),FALSE) == 0)
  105.     {
  106.         do_something_useful("dbsqlsend");
  107.     }
  108.  
  109.     /* Now, the network is available for writing. */
  110.  
  111.     dbsqlsend(dbproc);
  112.  
  113.     /* Now it's time to receive a response from the SQL Server. If the
  114.      * response isn't yet ready for some reason, we can again do something
  115.      * useful while we're waiting.
  116.      */
  117.  
  118.     while
  119.       (!(DBRBUF(dbproc))
  120.        && (ioselect(DBIORDESC(dbproc),TRUE) == 0))
  121.     {
  122.         do_something_useful("dbsqlok");
  123.     }
  124.  
  125.     /* Now, the response from the SQL Server is available. */
  126.  
  127.     dbsqlok(dbproc);
  128.  
  129.     /* From this point on, we will be alternately waiting for and 
  130.      * receiving information from the SQL Server. In each case, 
  131.      * we will try to do something useful whenever the information 
  132.      * is not yet available.
  133.      */
  134.  
  135.     while
  136.       (!(DBRBUF(dbproc))
  137.        && (ioselect(DBIORDESC(dbproc),TRUE) == 0))
  138.     {
  139.         do_something_useful("dbresults");
  140.     }
  141.  
  142.     dbresults(dbproc);
  143.  
  144.     printf("Database information:\n\n");
  145.  
  146.     while
  147.       (!(DBRBUF(dbproc))
  148.        && (ioselect(DBIORDESC(dbproc),TRUE) == 0))
  149.     {
  150.         do_something_useful("dbnextrow");
  151.     }
  152.  
  153.     while (dbnextrow(dbproc) != NO_MORE_ROWS)
  154.     {
  155.         strncpy
  156.             (namebuf, (char *)dbdata(dbproc, 1), dbdatlen(dbproc, 1));
  157.         namebuf[dbdatlen(dbproc, 1)] = '\0';
  158.         dbconvert(dbproc, SYBDATETIME, (dbdata(dbproc, 2)), -1, SYBCHAR, 
  159.               datebuf, -1);
  160.         printf("\t%s\t%s\n", namebuf, datebuf);
  161.     }
  162.  
  163.     dbexit();
  164.     exit(STDEXIT);
  165. }
  166.  
  167. void do_something_useful(str)
  168. char          *str;
  169. {
  170.     printf("Free time available while waiting to call \"%s\".\n", str);
  171. }
  172.  
  173. /*  ioselect(fd, readFlag)
  174.  *    informs the caller whether or not the file descriptor fd is ready
  175.  *    for reading or writing.
  176.  *  arguments:
  177.  *      fd        :  file descriptor to be checked
  178.  *      readFlag  :  TRUE  - for reading
  179.  *                   FALSE - for writing
  180.  *  returns:
  181.  *      0         :  if fd is not ready for reading or writing.
  182.  *      1         :  if fd is ready for  reading or writing.
  183.  *      Two implementations are provided: one using the System V poll call,
  184.  *      and the other one using the BSD 4.2 select call.         
  185.  */
  186. #if POLL_CALL
  187. /*  function definition using System V poll call */
  188. int ioselect(fd,readFlag)
  189. int fd;
  190. DBBOOL readFlag;
  191. {
  192.     struct pollfd   pollFds;
  193.  
  194.     pollFds.fd = fd;
  195.  
  196.     /* Set the requested event(s), depending on the boolean readFlag. */
  197.  
  198.     pollFds.events = ((readFlag == TRUE) ? (POLLIN | POLLPRI) : POLLOUT);
  199.  
  200.     return( poll (&pollFds, (unsigned long) 1, (int) 0));
  201. }
  202. #else
  203. /*  function definition using BSD select() call */
  204. int ioselect(fd,readFlag)
  205. int fd;
  206. DBBOOL readFlag;
  207. {
  208.     int readMask, writeMask;
  209.     struct timeval timeout;
  210.     int returnValue;
  211.  
  212.     /* Set for no wait time. */
  213.  
  214.     timeout.tv_sec = (unsigned long) 0;
  215.     timeout.tv_usec = (long) 0;
  216.  
  217.     if(readFlag == TRUE)
  218.     {
  219.         /* for reading */
  220.         readMask = 1 << fd;
  221.         returnValue =  select ((int) fd+1, &readMask, (int *)NULL,
  222.                                             (int *)NULL, &timeout);
  223.     }
  224.     else
  225.     {
  226.         /* for writing */
  227.         writeMask = 1 << fd;
  228.         returnValue =  select ((int) fd+1, (int *)NULL, &writeMask,
  229.                                            (int *)NULL, &timeout);
  230.     }
  231.  
  232.     return(returnValue);
  233. }
  234. #endif
  235.  
  236. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  237. DBPROCESS       *dbproc;
  238. int             severity;
  239. int             dberr;
  240. int             oserr;
  241. char            *dberrstr;
  242. char            *oserrstr;
  243. {
  244.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  245.         return(INT_EXIT);
  246.     else 
  247.     {
  248.         printf("DB-Library error:\n\t%s\n", dberrstr);
  249.  
  250.         if (oserr != DBNOERR)
  251.             printf("Operating-system error:\n\t%s\n", oserrstr);
  252.  
  253.         return(INT_CANCEL);
  254.     }
  255. }
  256.  
  257. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  258.                 srvname, procname, line)
  259.  
  260. DBPROCESS       *dbproc;
  261. DBINT           msgno;
  262. int             msgstate;
  263. int             severity;
  264. char            *msgtext;
  265. char            *srvname;
  266. char            *procname;
  267. DBUSMALLINT     line;
  268.  
  269. {
  270.     printf ("Msg %ld, Level %d, State %d\n", 
  271.             msgno, severity, msgstate);
  272.  
  273.     if (strlen(srvname) > 0)
  274.         printf ("Server '%s', ", srvname);
  275.     if (strlen(procname) > 0)
  276.         printf ("Procedure '%s', ", procname);
  277.     if (line > 0)
  278.         printf ("Line %d", line);
  279.  
  280.     printf("\n\t%s\n", msgtext);
  281.  
  282.     return(0);
  283. }
  284.